//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//                                                                              //
//  Author: meisq                                                               //
//          msq@qq.com                                                          //
//          ALINX(shanghai) Technology Co.,Ltd                                  //
//          heijin                                                              //
//     WEB: http://www.alinx.cn/                                                //
//     BBS: http://www.heijin.org/                                              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
// Copyright (c) 2017,ALINX(shanghai) Technology Co.,Ltd                        //
//                    All rights reserved                                       //
//                                                                              //
// This source file may be used and distributed without restriction provided    //
// that this copyright statement is not removed from the file and that any      //
// derivative work contains the original copyright notice and the associated    //
// disclaimer.                                                                  //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

//==========================================================================
//  Revision History:
//  Date          By                    Revision    Change Description
//--------------------------------------------------------------------------
//  2017/6/19     meisq                 1.0         Original
//  2020/1/28     deepcreatic_XiaoSiyu  2.0         Rewrite
//*************************************************************************/
module seg_scan(clk, rst_n, seg_sel, seg_data_out, seg_data_in);

genvar i;
parameter  SEG_FIGURES = 6;
parameter  SEG_WORD_WIDTH = 8;
localparam SEG_DATA_IN_FIGURES = SEG_FIGURES*SEG_WORD_WIDTH;
localparam SEG_SEL_WIDTH = $clog2(SEG_FIGURES);
localparam BLANK_SEL_CODE = {(SEG_FIGURES){1'b1}};
localparam BLANK_WORD_CODE = {(SEG_WORD_WIDTH){1'b1}};

parameter  SCAN_FREQ   = 200;       //scan frequency
parameter  CLK_FREQ    = 50000000;  //clock frequency
localparam SCAN_COUNT  = CLK_FREQ /(SCAN_FREQ * 6) - 1;

input clk;
input rst_n;
output reg[SEG_FIGURES-1:0]     seg_sel;            //digital led chip select
output reg[SEG_WORD_WIDTH-1:0]  seg_data_out;       //eight segment digital tube output,MSB is the decimal point
input [SEG_DATA_IN_FIGURES-1:0] seg_data_in;

wire [SEG_WORD_WIDTH-1:0] seg_data [SEG_FIGURES-1:0];
generate
	for(i=0; i<SEG_FIGURES; i=i+1)
	begin: seg_data_in_assign_loop
		assign seg_data[i] = seg_data_in[(i+1)*SEG_WORD_WIDTH-1:i*SEG_WORD_WIDTH];
	end
endgenerate

reg [31:0] scan_timer;                  //scan time counter
reg [SEG_SEL_WIDTH-1:0] scan_sel;       //Scan select counter
always@(posedge clk or negedge rst_n)
begin
	if(rst_n == 1'b0)
	begin
		scan_timer <= 0;
		scan_sel <= 0;
	end
	else if(scan_timer >= SCAN_COUNT)
	begin
		scan_timer <= 0;
		if(scan_sel >= SEG_FIGURES-1)
			scan_sel <= 0;
		else
			scan_sel <= scan_sel + 1'd1;
	end
	else
	begin
		scan_timer <= scan_timer + 1'd1;
	end
end

always@(posedge clk or negedge rst_n)
begin
	if(rst_n == 1'b0)
	begin
		seg_sel <= BLANK_SEL_CODE;
		seg_data_out <= BLANK_WORD_CODE;
	end
	else
	begin
		seg_sel <= BLANK_SEL_CODE;
		if (scan_sel<SEG_FIGURES)
		begin
			seg_sel[scan_sel] <= 0;
			seg_data_out <= seg_data[scan_sel];
		end
		else
		begin
			seg_data_out <= BLANK_WORD_CODE;
		end
	end
end

endmodule